package com.orientation.h5controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.orientation.commom.Result;
import com.orientation.commom.exception.CustomException;
import com.orientation.dto.UserLoginParamDto;
import com.orientation.entity.Article;
import com.orientation.entity.Bulletin;
import com.orientation.entity.Student;
import com.orientation.mapper.ArticleMapper;
import com.orientation.service.ArticleCateService;
import com.orientation.service.ArticleService;
import com.orientation.service.BulletinService;
import com.orientation.service.StudentService;
import com.orientation.utils.PageUtils;
import com.orientation.vo.ReportSheetVo;
import com.orientation.vo.StudentClassInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @PROJECT_NAME: orientation
 * @PACKAGE_NAME: com.orientation.h5controller
 * @NAME: AppStudentController
 * @author:xuandian
 * @DATE: 2022/1/13 4:18
 * @description: 小程序接口
 */
@Api(tags = "小程序学生接口")
@Validated
@RestController
@RequestMapping("/api")
public class AppStudentController {
    @Resource
    private StudentService studentService;
    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private ArticleCateService articleCateService;
    @Resource
    private BulletinService bulletinService;

    @ApiOperation("学生登录")
    @PostMapping("/student/login")
    public ResponseEntity<Result> login(@RequestBody UserLoginParamDto loginParamDto) {
        Student ret = studentService.login(loginParamDto);
        return ResponseEntity.ok(Result.success(ret));
    }

    @ApiOperation("修改")
    @PostMapping("/student/updateStudent")
    public ResponseEntity<Result> updateStudent(@RequestBody Student student) {
        studentService.updateStudent(student);
        return ResponseEntity.ok(Result.success());
    }

    @ApiOperation("基本信息")
    @GetMapping("/student/queryCourseClassInfo")
    public ResponseEntity<Result> queryCourseClassInfo(@RequestParam(value = "studentId") String studentId) {
        List<StudentClassInfoVo> studentClassInfoVos = studentService.getStudentClassInfoVos(null, null, null, null, null, "1", studentId);
        StudentClassInfoVo courseClassInfoVo = new StudentClassInfoVo();
        if (!CollectionUtils.isEmpty(studentClassInfoVos)) {
            courseClassInfoVo = studentClassInfoVos.get(0);
        } else {
            throw new CustomException("18", "不存在该学生新信息暂无数据");
        }
        return ResponseEntity.ok(Result.success(courseClassInfoVo));
    }

    @ApiOperation("学生信息")
    @GetMapping("/student/queryStudent")
    public ResponseEntity<Result> queryStudent(@RequestParam(value = "sid") String sid) {
        Student studentServiceOne = studentService.getOne(Wrappers.<Student>lambdaQuery().eq(Student::getSid, sid));
        return ResponseEntity.ok(Result.success(studentServiceOne));
    }

    @ApiOperation("报道单")
    @GetMapping("/student/studentReport")
    public ResponseEntity<Result> studentReport(@RequestParam(value = "studentId") String studentId, @RequestParam(value = "idCardNo", required = false) String idCardNo) {
        ReportSheetVo reportSheetVo = studentService.studentReport(studentId, idCardNo);
        return ResponseEntity.ok(Result.success(reportSheetVo));
    }

    @ApiOperation("修改头像")
    @PostMapping("/student/updateStudentAvatar")
    public ResponseEntity<Result> updateStudentAvatar(@RequestParam(value = "files") MultipartFile[] files,
                                                      @RequestParam(value = "sid") String sid) {
        Student student = studentService.updateStudentAvatar(files, sid);
        return ResponseEntity.ok(new Result(student));
    }


    @ApiOperation("校园风光")
    @GetMapping("/article/app/news")
    public List<String> getSchoolNew() {
        List<String> strings = articleService.list(Wrappers.<Article>lambdaQuery().eq(Article::getCid, 4)
                .orderByDesc(Article::getCreateTime).last("limit 2")).stream().map(Article::getContent).collect(Collectors.toList());
        // 根据类型取10条
        StringBuilder sb = new StringBuilder();
        for (String s : strings) {
            sb.append(s);
            sb.append(",");
        }
        return Arrays.asList(sb.toString().split(","));
    }

    @ApiOperation("校园风光//公告//文化")
    @GetMapping("/article/getContent")
    public List<Article> getContent(@RequestParam(value = "cid", defaultValue = "1") Integer cid) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(String.valueOf(cid)) && cid == 4) {
            queryWrapper.ne(Article::getCid, 4).ne(Article::getCid, 1).ne(Article::getCid, 2).ne(Article::getCid, 3);
        } else {
            queryWrapper.eq(Article::getCid, cid);
        }
        queryWrapper.orderByDesc(Article::getCreateTime).eq(Article::getReleaseType, 1);
        List<Article> articleList = articleMapper.selectList(queryWrapper).stream().sorted(Comparator.comparing(Article::getTitle)).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(articleList)){
            articleList = articleList.stream().sorted(Comparator.comparing(Article::getNewsTime).thenComparingInt(Article::getHits)).collect(Collectors.toList());
        }

        return articleList;
    }

    @ApiOperation("校园风光//公告//文化")
    @GetMapping("/article/getContentpage")
    public IPage<Article> getContentpage(@RequestParam(value = "cid", defaultValue = "1") Integer cid,
                                         @RequestParam(required = false, defaultValue = "1") Integer currentPage,
                                         @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(String.valueOf(cid)) && cid == 4) {
            queryWrapper.ne(Article::getCid, 4).ne(Article::getCid, 1).ne(Article::getCid, 2).ne(Article::getCid, 3);
        } else {
            queryWrapper.eq(Article::getCid, cid);
        }
        queryWrapper.orderByDesc(Article::getCreateTime).eq(Article::getReleaseType, 1);
        List<Article> articleList = articleMapper.selectList(queryWrapper).stream().sorted(Comparator.comparing(Article::getTitle)).collect(Collectors.toList());
        articleList = articleList.stream().sorted(Comparator.comparing(Article::getNewsTime).thenComparingInt(Article::getHits)).collect(Collectors.toList());
        return PageUtils.getPage(articleList, currentPage, pageSize);
    }

    @ApiOperation("校园风光//公告//文化")
    @GetMapping("/article/getContentOne")
    public Article getContentOne(@RequestParam(value = "sid", defaultValue = "1") String sid) {
        Article articles = articleService.getOne(Wrappers.<Article>lambdaQuery().eq(Article::getSid, sid));
        if (!ObjectUtils.isEmpty(articles)) {
            if (articles.getHits() != null && articles.getHits() > 0) {
                articles.setHits(articles.getHits() + 1);
            } else {
                articles.setHits(1);
            }
            articleService.updateById(articles);
        }

        return articles;
    }

    @ApiOperation("校园风光")
    @GetMapping("/article/getAllCampus")
    public List<Article> getAllCampus(@RequestParam(value = "title") String title,
                                      @RequestParam(value = "cid") String cid) {
        List<Article> articles = articleService.list(Wrappers.<Article>lambdaQuery().eq(Article::getCid, cid)
                .like(StringUtils.isNotEmpty(title), Article::getTitle, title).orderByDesc(Article::getCreateTime));
        articles = articles.stream().sorted(Comparator.comparing(Article::getTitle)).collect(Collectors.toList());
        return articles;
    }

    @ApiOperation("我的通知通告")
    @GetMapping("/bulletin/queryBulletin")
    public ResponseEntity<Result> queryBulletin(@RequestParam(value = "studentId") String studentId) {
        List<Bulletin> bulletins = bulletinService.list(Wrappers.<Bulletin>lambdaQuery().eq(Bulletin::getStudentId, studentId)).stream().sorted(Comparator.comparingInt(Bulletin ::getViewState)).collect(Collectors.toList());
        return ResponseEntity.ok(Result.success(bulletins));
    }

    @ApiOperation("单个通知通告")
    @GetMapping("/bulletin/bySid")
    public ResponseEntity<Result> bulletins(@RequestParam(value = "sid") String sid) {
        Bulletin bulletins = bulletinService.getById(sid);
        return ResponseEntity.ok(Result.success(bulletins));
    }


    @ApiOperation("单个通知通告已读回复")
    @GetMapping("/bulletin/editbySid")
    public ResponseEntity<Result> editbySid(@RequestParam(value = "sid") String sid) {
        Bulletin bulletins = bulletinService.getById(sid);
        bulletins.setViewState(1);
        bulletinService.updateById(bulletins);
        return ResponseEntity.ok(Result.success());
    }

}
